package ThreadBasic.other;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @author yyh
 * @create 2020-11-10 8:48
 */
public class SemaphoreDemo {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3);

        for (int i = 1; i <= 6 ; i++) {
            new Thread(() -> {
                try{
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+"抢到车位");

                    TimeUnit.SECONDS.sleep(2);
                    System.out.println(Thread.currentThread().getName()+"离开车位");
                }catch(Exception e){
                    e.printStackTrace();
                }finally{
                    semaphore.release();
                }
            }).start();
        }
    }
}
